package cn.zyb._02_sync;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @author zyb
 * @version 1.0.0
 * @date 2022年08月02日 18:36:38
 * @packageName cn.zyb._02_sync
 * @className Producer
 * @describe TODO
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        // 1.创建消费者对象
        DefaultMQProducer producer = new DefaultMQProducer("hello_producer_group");
        // 2.连接mq
        producer.setNamesrvAddr("127.0.0.1:9876");

        producer.setSendMsgTimeout(6000);
        // 3.启动消费者
        producer.start();
        // 4.发送异步消息
        Message message = new Message("hello_topic2", "hello_tags2", "我在源码学VUE！".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });
    }
}
